﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shumei.EFCore.SimpleService;
using Shumei.EFCore.Model;

namespace source.Areas.Admin.Controllers
{
    public class CoursesController : Controller
    {
        CourseService courseService;
        CourseVideoService videoService;

        public CoursesController()
        {
            courseService = new CourseService();
        }

        /// <summary>
        /// 获取课程列表
        /// </summary>
        /// <returns></returns>
        public ActionResult CoursesList(int page=1,int num = 10)
        {
            int totalPage;
            var res = courseService.GetCoursesList(page, out totalPage, num);
            ViewBag.page = page;
            ViewBag.totalPage = totalPage;
            return View("CoursesList", res);
        
        }
        /// <summary>
        /// 获取课程类型
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCourseType()
        {
            Product_TypeService courseTypesService = new Product_TypeService();
            return Json(courseTypesService.getList(), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 打开课程上传界面（即添加新的课程）
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadCourse()
        {
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddCourse (Course newModel)
        {
            newModel.UploadTime = DateTime.Now;
            //newModel.user = AccountController.GetUserInfo(this.HttpContext);
            try
            {
                courseService.Add(newModel, true);
                return RedirectToAction("CoursesList");
            }
            catch
            {

            }
            return null;

        }

        public ActionResult EditCourse(int id)
        {
            var _course = courseService.GetCourseDetail(id);
            if (courseService != null)
            {
                return View("EditCourse", _course);
            }
            return null;
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditCourse(Course model)
        {
            courseService.Update(model, true);
            return RedirectToAction("CoursesList");
        }

        public ActionResult DeleteCourse(int id)
        {
            try
            {
                var res = courseService.GetCourseDetail(id);
                if (res != null)
                    courseService.Delete(res);
                return RedirectToAction("CoursesList");
            }
            catch
            {

            }
            return null;
        }



        /// <summary>
        /// 打开UpLoad页面(上传视频)
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadVideo()
        {
            return View();
        }
        //EditVideo   DeleteVideo

        [HttpGet]
        public ActionResult EditVideo(int id)
        {
            //var _new = newsService.GetNewDetail(id);
            var _video = videoService.GetCourseVideoDetail(id);
            if (_video != null)
            {
                return View("EditVideo", _video);
            }
            return null;
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditVideo(CourseVideo model)
        {

            videoService.Update(model, true);
            return RedirectToAction("EditCourse");  //返回到课程编辑界面
        }

        public ActionResult DeleteVideo(int id)
        {
            try
            {
                var res = videoService.GetCourseVideoDetail(id);
                if (res != null)
                    videoService.Delete(res);
                return RedirectToAction("EditCourse");
            }
            catch
            {

            }
            return null;
        }
	}
}